﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using JigLibX.Collision;
using JigLibX.Physics;
using JigLibX.Geometry;
using JigLibX.Math;

namespace DARE
{
    public class CPhysicBox : APhysicObject
    {

    #region fields

        private float m_x;
        private float m_y;
        private float m_z;

    #endregion

    #region properties

        public Vector3 Dimensions { get { return new Vector3(m_x, m_y, m_z); } }

    #endregion

    #region ctor

        public CPhysicBox()
        {
            Initialize(Vector3.Zero, Matrix.Identity, Vector3.One);
        }

        public CPhysicBox(Vector3 size)
        {
            Initialize(Vector3.Zero, Matrix.Identity, size);
        }

        public CPhysicBox(float x, float y, float z)
        {
            Initialize(Vector3.Zero, Matrix.Identity, new Vector3(x, y, z));
        }

        public CPhysicBox(Vector3 position, Matrix orientation, Vector3 dimension)
        {
            Initialize(position, orientation, dimension);
        }

        private void Initialize(Vector3 position, Matrix orientation, Vector3 dimension)
        {
            m_x = dimension.X;
            m_y = dimension.Y;
            m_z = dimension.Z;

            m_body = new Body();
            m_collisionSkin = new CollisionSkin(m_body);
            m_body.CollisionSkin = m_collisionSkin;

            // bounding box position / orientation / scale
            m_scale = dimension;
            Box box = new Box(position, orientation, m_scale);
            m_collisionSkin.AddPrimitive(box, new MaterialProperties(0.8f, 0.8f, 0.7f));

            Vector3 com = SetMass(1.0f);
            m_collisionSkin.ApplyLocalTransform(new Transform(-com, Matrix.Identity));
            m_body.EnableBody();

            m_basicModel = new CBoxModel("mBox", dimension.X, dimension.Y, dimension.Z);
        }

        public override APhysicObject Clone()
        {
            CPhysicBox clone = new CPhysicBox(m_x, m_y, m_z);

            base.CopyPropertiesTo(clone);
            return clone;
        }

    #endregion

    }
}
